# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-yaml
_pkgname=yaml
pkgver=1.1.2
pkgrel=7
pkgdesc="LibYaml binding for Lua"
url="https://lubyk.github.io/lubyk/yaml.html"
arch="all"
license="MIT"
makedepends="cmake samurai yaml-dev"
source="$pkgname-$pkgver.tar.gz::https://github.com/lubyk/$_pkgname/archive/REL-$pkgver.tar.gz
	cmake.patch"
builddir="$srcdir/$_pkgname-REL-$pkgver"
options="!check"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-$_pkgname:_subpackage"
done

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		cmake -B build/$lver -G Ninja \
			-DCMAKE_C_FLAGS="$CFLAGS -Wall -DNDEBUG $(pkg-config --cflags lua$lver)" \
			-DCMAKE_SKIP_RPATH=TRUE \
			-DCMAKE_VERBOSE_MAKEFILE=TRUE \
			-DCMAKE_BUILD_TYPE=MinSizeRel \
			-DLUA_INSTALL_DIR=/usr/share/lua/$lver \
			-DLUA_INSTALL_BINDIR=/usr/lib/lua/$lver
		cmake --build build/$lver
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-1"
	pkgdesc="$pkgdesc $lver"
	depends="$depends lua$lver lua$lver-lub"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	[ "$lver" = 5.1 ] && replaces="$pkgname"

	DESTDIR="$subpkgdir" cmake --install "$builddir"/build/$lver

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="
9d2ff4ddc243922f11c2ebea1d45bb3fa02e96a30752c04e65aec236e76780780511fe605a7a0bdbe9764189764a3951aadff8bb028c402571fd052cd70e35ac  lua-yaml-1.1.2.tar.gz
db024b2acfa88441a4a72ec9c2a42d0980465cd759815474f02e3912ca458d913317aacdecf911e4048d43bab7878f472ce044f57dac3df66d20752e1255a8a8  cmake.patch
"
